package com.viish.android.apps.buildbot;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;

public class ManagerActivity extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        linkButtonsWithActions();
        
        //When the user lauch the app for the first time, we display a message
        displayWelcomePopup();
        
        // If no buildbot configured, we invite our user to configure one
        displayConfigurationPopup();
    }
    
    private void displayWelcomePopup()
    {
    	final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean firstTime = prefs.getBoolean("firsttime", true);
        if (firstTime)
        {
	    	AlertDialog.Builder popup = new AlertDialog.Builder(this);
	    	popup.setTitle("Android Buildbot Manager");
	    	popup.setMessage(getString(R.string.welcome));
	    	popup.setNegativeButton("Back", new OnClickListener() 
	    	{
				public void onClick(DialogInterface dialog, int which) 
				{
			    	SharedPreferences.Editor editor = prefs.edit();
			    	editor.putBoolean("firsttime", false);
			    	editor.commit();
				}
			});
	    	popup.create();
	    	popup.show();
        }
    }
    
    private void displayConfigurationPopup()
    {
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String host = prefs.getString("host", "");
        if (host.equals("")) 
        {
        	AlertDialog.Builder popup = new AlertDialog.Builder(this);
        	popup.setTitle("No buildbot configured yet");
        	popup.setMessage("You don't have configured a buildbot yet. Do you want to do it now ?");
        	popup.setPositiveButton("Configure it now", new OnClickListener() 
        	{
				public void onClick(DialogInterface dialog, int which) 
				{
					startPreferencesActivity();
				}
			});
        	popup.setNegativeButton("Configure it later", null);
        	popup.create();
        	popup.show();
        }
    }
    
    private void displayAboutPopup()
    {
    	AlertDialog.Builder popup = new AlertDialog.Builder(this);
    	popup.setTitle("About Android Buildbot Manager");
    	popup.setMessage(getString(R.string.about));
    	popup.setNegativeButton("Back", null);
    	popup.create();
    	popup.show();
    }
    
    private void startPreferencesActivity()
    {
    	Intent i = new Intent();
		i.setClass(ManagerActivity.this, PreferencesActivity.class);
		startActivity(i);
    }
    
    private void linkButtonsWithActions()
    {
    	Button bWaterfall = (Button) findViewById(R.id.Waterfall);
    	Button bPreferences = (Button) findViewById(R.id.Preferences);
    	Button bAbout = (Button) findViewById(R.id.About);
    	
    	bWaterfall.setOnClickListener(new View.OnClickListener() 
    	{
			public void onClick(View v) 
			{
				Intent i = new Intent();
				i.setClass(ManagerActivity.this, WaterfallActivity.class);
				startActivity(i);
			}
		});
    	
    	bPreferences.setOnClickListener(new View.OnClickListener() 
    	{
			public void onClick(View v) 
			{
				startPreferencesActivity();
			}
		});
    	
    	bAbout.setOnClickListener(new View.OnClickListener() 
    	{
			public void onClick(View v) 
			{
				displayAboutPopup();
			}
		});
    }
}